package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.util.JSONResult;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class ExceptionController {
    @ExceptionHandler(UnauthorizedException.class)
    public void handlerException(HandlerMethod handlerMethod, HttpServletResponse response) throws IOException {
        ResponseBody annotation = handlerMethod.getMethodAnnotation(ResponseBody.class);
        if(annotation != null){
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().print(JSON.toJSONString(JSONResult.mark("您没有权限执行该操作")));
        }else{
            response.sendRedirect("/nopermission.jsp");
        }
    }
}
